home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / FREENET / WWW / UPDATE.PL < prev   
Perl Script  |  1998-05-09  |  5KB  |  156 lines

  1. #!/usr/bin/perl
  2.  
  3. # ls -lRe list date checker
  4. # Simon Pollard, Apr 98
  5. # No rights even remotely reserved, do whatever you like
  6.  
  7. if($#ARGV != 1) {
  8.   print "Format: update.pl <directory to scan> <html output filename>\n";
  9.   exit 1;
  10. }
  11.  
  12. if(! -d $ARGV[0]) {
  13.   print "That directory ($ARGV[0]) doesn't exist.\n";
  14.   exit 1;
  15. }
  16. $sourceDir = $ARGV[0]; # the directory to 'ls'
  17. $outputDir = (getpwnam('ftpadmin'))[7] . "/scripts/output"; # the output here
  18. $scriptDir = (getpwnam('ftpadmin'))[7] . "/scripts"; # script + template
  19.  
  20. $template = "$scriptDir/updateTmpl";
  21. $html = "$outputDir/$ARGV[1].html";
  22. $exclude = "$outputDir/excludes";
  23.  
  24. $fileroot = "http://freenet.barnet.ac.uk/$ARGV[1]/";
  25.  
  26. %months = ("Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04",
  27.            "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08",
  28.            "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12");
  29.  
  30. chdir($sourceDir);
  31.  
  32. # assumptions - permissions are 10 chars long, dates are 24 chars long
  33. # *** files cannot have # character in them ***
  34.  
  35. # open a pipe from the ls command
  36. open(SRC,"/bin/ls -lRe |") || die "Error opening ls output: $!";
  37.  
  38. open(TEMPL,"<$template") || die "Error opening template file: $!";
  39. open(OUT,">$html") || die "Error opening HTML output file: $!";
  40.  
  41. open(EXCLUDES,"<$exclude") || die "Error opening excludes file: $!";
  42. while(<EXCLUDES>) { chop; push(@excludes,$_); }
  43. close(EXCLUDES);
  44.  
  45. while(<TEMPL>) {
  46.   last if /^OUTPUT HERE$/;
  47.   print OUT ;
  48. }
  49.  
  50. # get current date
  51.  
  52. ($sec,$min,$hr,$mday,$mn,$yr) = localtime();
  53. $lyr = $yr + 1900; $lmn = $mn; $ldy = $mday;
  54. ## $lmn--;  - you don't have to do this, because months are numbered
  55. ##            from 0 when you use localtime()             
  56. if($lmn == 0) { $lmn = 12; $lyr--; }
  57.  
  58. if($lmn < 10) { $lmn = "0$lmn"; }
  59. if($ldy < 10) { $ldy = "0$ldy"; }
  60. if($hr < 10) { $hr = "0$hr"; }
  61. if($min < 10) { $min = "0$min"; }
  62. if($sec < 10) { $sec = "0$sec"; }
  63. # trust me, it's all necessary
  64. # the time/date 'keys' must all be the same length
  65. $lkey = "$lyr$lmn$ldy$hr$min$sec";
  66.  
  67. # stuff at the top of the page
  68.  
  69. $what = $ARGV[1];
  70. $what =~ s/^(.)(.+)$/\u$1$2/;
  71.  
  72. print OUT "<h3><u>$what uploads for the past month (since $mday/$lmn/$lyr)</u></h3>\n";
  73.  
  74. print OUT <<TBLHD;
  75. <table>
  76. <tr>
  77.  <th align="left">Author</th><th align="right">Size</th><th colspan=2>Date Updated</th><th align="left">File</th>
  78. </tr>
  79. TBLHD
  80.  
  81. if(eof(SRC)) { print "No files.  Exiting.\n"; exit 1; }
  82.  
  83. # read in ls -lRe
  84.  
  85. $auth = "<none>"; $rest = "";
  86.  
  87. while(<SRC>) {
  88.   if(/^$/) { # (a blank line)
  89.     $auth = "<none>";
  90.     $rest = "";
  91.     next;
  92.   }
  93.   if(($thisauth) = /^(\S+):$/) { # if the line looks like '<something>:'
  94.     $auth = $thisauth;           # the author is <something>
  95.     if($thisauth =~ tr/\//\//) { # if there's some /s in it
  96.       ($auth) = ($thisauth =~ /^([^\/]+)\//); # the author's the stuff b4 them
  97.       ($rest) = ($thisauth =~ /^$auth\/(.+)$/); # and the rest is what's after
  98.       $rest .= "/";
  99.     }
  100.     next;
  101.   }
  102.   ($perm,$ln,$usr,$grp,$size,$date,$name) = # pretty eh? don't you love perl :)
  103.     /^(.{10})\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(.{24})\s+(.+)$/;
  104.   if(($perm =~ /^-/) && ($auth ne "<none>") && (! grep(($name eq $_),@excludes))) {
  105.                      # it's a file with an author which isn't excluded
  106.     push(@out,"$auth#$rest$name#$size#$date"); # add line to the array
  107.   }
  108. }
  109.  
  110. close(SRC);
  111.  
  112. # ok, we have a nice array of files.  now to sort 'em by date.
  113.  
  114. foreach (@out) {
  115.   # first, make a nice date numbery thing to sort them by
  116.   ($auth,$file,$size,$date) = split('#',$_);
  117.   ($wkdy,$mo,$dy,$hr,$mn,$sc,$yr) = ($date =~ /^(\w+) (\w+)\s+(\d+) (\d+):(\d+):(\d+) (\d+)$/);
  118.   if($dy < 10) { $dy="0$dy"; }
  119.   $mono = $months{$mo}; # find the month number from the month name
  120.   if(! $mono) {
  121.     print "Corrupt file or a new month has been invented.\n$_"; # you'd hope not ;)
  122.     exit 1;
  123.   }
  124.   $keydate = "$yr$mono$dy$hr$mn$sc"; # pretty key. looks like 19980418153025
  125.   if($keydate > $lkey) {             # (Apr 18th 1998, 15:30.25)
  126.     $final{$keydate} = "$auth#$file#$size#$mo $dy#$hr:$mn";
  127.   }
  128. }
  129.  
  130. @sortout = sort {$b <=> $a} keys %final;
  131.  
  132. # finally, the output!
  133. # prettifying and html'ing
  134.  
  135. foreach $key (@sortout) {
  136.   ($auth,$file,$size,$date,$time) = split('#',$final{$key});
  137.   print OUT "<tr><td>$auth</td>";
  138.   $type = "b";
  139.   if($size > 1024) { $size /= 1024; $type = "k"; }
  140.   if($size > 1024) { $size /= 1024; $type = "M"; }
  141.   if($type eq "b") { $sz = $size; } else { $sz = sprintf("%.2f",$size); }
  142.   print OUT "<td align=\"right\">$sz$type</td>";
  143.   print OUT "<td>$date</td><td>$time</td>";
  144.   print OUT "<td><a href=\"$fileroot$auth/$file\">$file</a></td></tr>\n";
  145. }
  146.  
  147. print OUT "</table>\n";
  148.  
  149. print OUT "<hr><p>This file was auto generated on ",`date`,"</p>\n";
  150.  
  151. while(<TEMPL>) { print OUT ; }
  152.  
  153. close(TEMPL); close(OUT);
  154.  
  155. # the end :)
  156.